package com.eliza.room.db.activity

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.eliza.room.R
import com.eliza.room.model.Student


class RoomRecycleAdapter : RecyclerView.Adapter<RoomRecycleAdapter.MyViewHolder> {
    lateinit var stuList: List<Student>

    constructor(stuList: List<Student>) : super() {
        this.stuList = stuList
    }


    class MyViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val root: View =
            LayoutInflater.from(parent.context)
                .inflate(
                    R.layout.activity_recycle_item,
                    parent,
                    false
                )
        return MyViewHolder(root)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val student: Student = stuList.get(position)
        val tvId: TextView = holder.itemView.findViewById(R.id.tvId)
        val tvName: TextView = holder.itemView.findViewById(R.id.tvName)
        val tvAge: TextView = holder.itemView.findViewById(R.id.tvAge)

        tvAge.text = student.userAge.toString()
        tvName.text = student.userName.toString()
        tvId.text = student.id.toString()


    }

    override fun getItemCount(): Int = stuList.size

}